package com.star.system.controller;

import com.star.core.base.BaseController;
import com.star.security.annotations.Authorize;
import com.star.system.dto.MenuSaveDTO;
import com.star.system.dto.MenuUpdateDTO;
import com.star.system.service.MenuService;
import com.star.system.vo.MenuTreeVO;
import com.star.system.vo.MenuUserTreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单
 *
 * @author yangzj
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    /**
     * 保存
     *
     * @param dto dto
     */
    @Authorize("menu:all")
    @PostMapping
    public void save(@Validated @RequestBody MenuSaveDTO dto) {
        menuService.save(dto);
    }

    /**
     * 更新
     *
     * @param dto dto
     */
    @Authorize("menu:all")
    @PutMapping
    public void update(@Validated @RequestBody MenuUpdateDTO dto) {
        menuService.update(dto);
    }

    /**
     * 删除
     *
     * @param ids id
     */
    @Authorize("menu:all")
    @DeleteMapping
    public void deleteByIds(@RequestBody List<Long> ids) {
        menuService.deleteByIds(ids);
    }

    /**
     * 所有菜单树
     *
     * @return {@link List}<{@link MenuTreeVO}>
     */
    @Authorize("menu:all")
    @GetMapping("/tree")
    public List<MenuTreeVO> tree() {
        return menuService.tree();
    }

    /**
     * 用户菜单树
     *
     * @return {@link List}<{@link MenuUserTreeVO}>
     */
    @GetMapping("/user-tree")
    public List<MenuUserTreeVO> treeUser() {
        return menuService.treeUser(this.getUserId(), this.isSuperAdmin());
    }
}
